package com.example.shop.user.vo;

import com.example.shop.pojo.User;
import com.example.shop.user.dto.UserBriefInfo;
import com.example.shop.user.enums.UserState;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import lombok.Data;

import javax.validation.constraints.Email;
import javax.validation.constraints.Size;
import java.time.LocalDate;

@Data
public class UserPublicInfo extends UserBriefInfo {

    /**
     * 性别
     */
    private String sex;
    /**
     * 所在地区
     */
    private String area;

    @JsonDeserialize(using = LocalDateDeserializer.class)
    @JsonSerialize(using = LocalDateSerializer.class)
    //如果转换失效，要么没有导包joda-time
    @JsonFormat(pattern = "yyyy-MM-dd",timezone = "GMT+8")
    /**
     * 出生日期
     */
    private LocalDate dateBirth;
    /**
     * 用户账户
     */
    private String account;
    /**
     * 用户的电话
     */
    private String phone;
    @Email
    @Size(min = 3)
    /**
     * 用户的邮箱
     */
    private String email;
    /**
     * 人脸token
     */
    private String faceToken;
    /**
     * 个性签名
     */
    private String signature;
    /**
     * 用户状态
     */
    private UserState userState;
    /**
     * 注册时间
     */
    private LocalDate registerDate;


    /**
     * 根据用户转换成用户公开信息对象，即传递给前端的视图对象
     * @param user 用户信息
     * @return 返回用户公开信息对象
     */
    public static UserPublicInfo getUserPublicInfo(User user){
        if(user!=null){
            UserPublicInfo userPublicInfo = new UserPublicInfo();
            userPublicInfo.id = user.getUserId();
            userPublicInfo.setAvatar(user.getUserAvatar());
            userPublicInfo.name = user.getUserName();
            userPublicInfo.sex = user.getUserSex();
            userPublicInfo.area = user.getUserArea();
            userPublicInfo.dateBirth = user.getUserDateBirth();
            userPublicInfo.account = user.getUserAccount();
            userPublicInfo.phone = user.getUserPhone();
            userPublicInfo.email = user.getUserEmail();
            userPublicInfo.faceToken = user.getUserFaceToken();
            userPublicInfo.signature = user.getUserSignature();
            userPublicInfo.userState = UserState.getUserState(user.getUserState());
            userPublicInfo.registerDate = user.getUserRegisterDate();
            return userPublicInfo;
        }
        return null;
    }

}
